package com.weipai.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.DLeadFloorMapper;
import com.weipai.demo.mapper.base.LeadFloorMapper;
import com.weipai.demo.model.LeadFloor;
import com.weipai.demo.service.LeadFloorService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: 赵项阳
 * @Date: 2019/7/2 10:16
 * @Version 1.0
 */
@Service
public class LeadFloorServiceImpl implements LeadFloorService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private LeadFloorMapper leadFloorMapper;
    @Autowired
    private DLeadFloorMapper dLeadFloorMapper;

    //添加或修改资讯类别
    @Override
    public Response save(Integer id, String leadFloor, Integer status) {
        Response response=new Response();
        try {
            Date date=new Date();
            Date createTime=date;
            if (id!=null){
                dLeadFloorMapper.updateById(leadFloor,status,createTime,id);
                response.setCode(RetCode.UPDATESUCCESS);
            }else {
                LeadFloor floor=new LeadFloor();
                floor.setLeadFloor(leadFloor);
                floor.setStatus(status);
                floor.setCreateTime(createTime);
                int insert = leadFloorMapper.insert(floor);
                response.setCode(RetCode.SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return response;
    }

    //根据id删除
    @Override
    public Response deleteById(Integer id) {
        Response response=new Response();
        if (id!=null){
            dLeadFloorMapper.deleteById(id);
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.ID_NULL);
            return response;
        }
        return response;
    }

    //分页展示资讯类别
    @Override
    public LayuiTablePagingResult pageShow(Integer page, Integer size) {
        LayuiTablePagingResult result=null;
        try {
            PageHelper.startPage(page,size);
            List<LeadFloor> leadFloors = leadFloorMapper.selectAll();
            PageInfo<LeadFloor> pageInfo=new PageInfo<>(leadFloors);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("pageShow"+e.getMessage());
        }
        return result;
    }

    @Override
    public Response newsShow(Integer status) {
        Response response=new Response();
        try {
            List<LeadFloor> leadFloors = dLeadFloorMapper.selectAll(status);
            response.put("leadFloors",leadFloors);
            response.setCode(RetCode.SUCCESS);
        } catch (Exception e) {
            response.setData(e.getMessage());
            response.setCode(RetCode.REQUEST_ERROR);
            e.printStackTrace();
            logger.error("newsShow"+e.getMessage());
        }
        return response;
    }

    //

}
